package com.ge.healthcare.autosc.ui.prodiagsscheduler.controller;

/*
 * 
 * Copyright (C) 2011 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the
 * General Electric Company (GE). You shall not disclose this software and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES. 
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.ge.healthcare.autosc.dto.ScheduleBean;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.service.intf.CfgProDiagsScheduleService;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.service.intf.ModelTypePropService;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.service.intf.ProDiagsConnectivityService;

public class ProDiagsConnectivityController extends MultiActionController {

	private final Logger logger = Logger.getLogger(ProDiagsConnectivityController.class);
	private DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	private ProDiagsConnectivityService proDiagsConnectivityService;
	private CfgProDiagsScheduleService cfgProDiagsScheduleService;
	private ModelTypePropService modelTypePropService;

	public void setModelTypePropService(ModelTypePropService modelTypePropService) {
		this.modelTypePropService = modelTypePropService;
	}

	public void setCfgProDiagsScheduleService(
			CfgProDiagsScheduleService cfgProDiagsScheduleService) {
		this.cfgProDiagsScheduleService = cfgProDiagsScheduleService;
	}

	public void setProDiagsConnectivityService(ProDiagsConnectivityService proDiagsConnectivityService) {
		this.proDiagsConnectivityService = proDiagsConnectivityService;
	}
	
	/**
	 * Registers a custom date binder with Spring request handling mechanism
	 */
	@Override
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		super.initBinder(request, binder);
		dateFormat.setLenient(true);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	@SuppressWarnings("unchecked")
	public ModelAndView viewSchedule(HttpServletRequest request, HttpServletResponse response, ScheduleBean scheduleBean)
			throws IOException, Exception {
		ModelMap map = new ModelMap();
		map.put("schedules", proDiagsConnectivityService.loadSProdiagSchedule(scheduleBean));
		map.put("scheduleBean", scheduleBean);
		map.put("dayList", proDiagsConnectivityService.getDayList());
		map.put("minuteList", proDiagsConnectivityService.getMinuteList());
		map.put("hourList", proDiagsConnectivityService.getHourList());
		map.put("weekList", proDiagsConnectivityService.getWeekList());
		map.put("modalities", modelTypePropService.getAllModalities());
		return new ModelAndView("schedule", map);

	}

	@SuppressWarnings("unchecked")
	public ModelAndView filterSchedule(HttpServletRequest request, HttpServletResponse response, ScheduleBean scheduleBean)
			throws IOException, Exception {
		ModelMap map = new ModelMap();
		List<ScheduleBean> scheduleBeans = proDiagsConnectivityService.loadSProdiagSchedule(scheduleBean);
		map.put("schedules", scheduleBeans);
		map.put("scheduleBean", scheduleBean);
		map.put("dayList", proDiagsConnectivityService.getDayList());
		map.put("minuteList", proDiagsConnectivityService.getMinuteList());
		map.put("hourList", proDiagsConnectivityService.getHourList());
		map.put("weekList", proDiagsConnectivityService.getWeekList());
		map.put("showButtons", CollectionUtils.size(scheduleBeans)==1);
		map.put("modalities", modelTypePropService.getAllModalities());
		return new ModelAndView("schedule", map);

	}
	
	@SuppressWarnings("unchecked")
	public void getProductLines(HttpServletRequest request, HttpServletResponse response)
			throws IOException, Exception {
		String modality=request.getParameter("modality");
		List<String> productLineList=modelTypePropService.getProductLinesByModality(modality);
		try{       
			PrintWriter out = response.getWriter();
			out.println(StringUtils.join(productLineList.toArray(), ','));
			out.close();
			}
			catch(IOException ex){
				ex.printStackTrace();
			}
	}
	
	
	@SuppressWarnings("unchecked")
	public void getModelTypes(HttpServletRequest request, HttpServletResponse response)
			throws IOException, Exception {
		String modality=request.getParameter("modality");
		String productLine=request.getParameter("productLine");
		List<String> productLineList=modelTypePropService.getModelTypes(modality, productLine);
		try{       
			PrintWriter out = response.getWriter();
			out.println(StringUtils.join(productLineList.toArray(), ','));
			out.close();
			}
			catch(IOException ex){
				ex.printStackTrace();
			}
	}
	
	@SuppressWarnings("unchecked")
	public ModelAndView compareSchedule(HttpServletRequest request, HttpServletResponse response, ScheduleBean scheduleBean)
			throws IOException, Exception {
		ModelMap map = new ModelMap();
		map.put("compareSchedules", proDiagsConnectivityService.compareProDiagsConfig(scheduleBean));
		map.put("scheduleBean", scheduleBean);
		return new ModelAndView("scheduleCompare", map);

	}
	@SuppressWarnings("unchecked")
	public ModelAndView report(HttpServletRequest request, HttpServletResponse response, ScheduleBean scheduleBean)
			throws IOException, Exception {
		ModelMap map = new ModelMap();
		map.put("scheduleReport", proDiagsConnectivityService.report(scheduleBean));
		map.put("scheduleBean", scheduleBean);
		return new ModelAndView("scheduleReport", map);

	}
	@SuppressWarnings("unchecked")
	public ModelAndView applySchedule(HttpServletRequest request, HttpServletResponse response, ScheduleBean scheduleBean)
			throws IOException, Exception {
		ModelMap map = new ModelMap();
		map.put("scheduleBean", scheduleBean);
		return new ModelAndView("inProgress", map);

	}
	@SuppressWarnings("unchecked")
	public ModelAndView updateSchedule(HttpServletRequest request, HttpServletResponse response, ScheduleBean scheduleBean)
			throws IOException, Exception {
		
		cfgProDiagsScheduleService.updateSchedule(scheduleBean);
		ModelMap map = new ModelMap();
		return new ModelAndView("inProgress", map);

	}
}